﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;

namespace TopModel
{
    public class AnalyseHelper
    {
        private string m_connstr;
        private ProcHelper pHelper;

        public AnalyseHelper()
        {
            m_connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
            pHelper = new ProcHelper(m_connstr);
        }

        public AnalyseHelper(string connstr)
        {
            m_connstr = connstr;
            pHelper = new ProcHelper(m_connstr);
        }

        public List<Analysis> GetRegionStatistics(string sellerNick, ERateType rateType,ref int total)
        {
            try
            {
                List<Analysis> aList = null;
                DataTable dt = new DataTable();
                Analysis a = null;
                pHelper.ClearParameters();
                pHelper.AddParameters("@nick", SqlDbType.NVarChar, 10, sellerNick);
                pHelper.AddParameters("@rateType", SqlDbType.SmallInt, Convert.ToInt16(rateType));
                pHelper.AddParameters("@aysType", SqlDbType.SmallInt, Convert.ToInt16(EAnalysisType.Region));
                pHelper.AddParameters("@total", SqlDbType.Int, ParameterDirection.Output, total);
                dt = pHelper.GetDataTable("Analyse");
                total = Convert.ToInt32(pHelper.GetReturnValue("@total"));
                if (dt.Rows.Count > 0)
                {
                    aList = new List<Analysis>();
                    foreach (DataRow row in dt.Rows)
                    {
                        a = new Analysis() { Name = row["Name"].ToString(), Value = Convert.ToInt32(row["Value"]) };
                        aList.Add(a);
                    }
                }
                return aList;
            }
            catch (Exception e)
            {
                throw new Exception("以地区统计评价信息失败", e);
            }
        }

        public List<Analysis> GetProductStatistics(string sellerNick, ERateType rateType,ref int total)
        {
            try
            {
                List<Analysis> aList = null;
                DataTable dt = new DataTable();
                Analysis a = null;
                pHelper.ClearParameters();
                pHelper.AddParameters("@nick", SqlDbType.NVarChar, 10, sellerNick);
                pHelper.AddParameters("@rateType", SqlDbType.SmallInt, Convert.ToInt16(rateType));
                pHelper.AddParameters("@aysType", SqlDbType.SmallInt, Convert.ToInt16(EAnalysisType.Product));
                pHelper.AddParameters("@total", SqlDbType.Int, ParameterDirection.Output, total);
                dt=pHelper.GetDataTable("Analyse");
                total = Convert.ToInt32(pHelper.GetReturnValue("@total"));
                if (dt.Rows.Count > 0)
                {
                    aList = new List<Analysis>();
                    foreach (DataRow row in dt.Rows)
                    {
                        a = new Analysis() { Name = row["Name"].ToString(), Value = Convert.ToInt32(row["Value"]) };
                        aList.Add(a);
                    }
                }
                return aList;
            }
            catch (Exception e)
            {
                throw new Exception("以商品统计评价信息失败", e);
            }
        }
    }
}
